<?php

/**
 * \class html_elements_builder
 * \brief This class builds html content for basic functionalities.
 * \author  Salman Noor
 */
class html_elements_builder {

    /**
     * \brief Stores options for dropdown, radiobuttons etc.
     * \note it need to be cleared once utalised.
     */
    private $options = array();
    /**
     * \brief String that sets type of break space between form elements.
     * \notes break_space can include new line, tabs or carriage space.
     */
    private $break_space;
    /**
     * \brief String that set default selected options for the form element.
     */
    private $selected;
    /**
     * \brief This string is used by many form elements to specify it type.
     * \note For example button type can be set to reset or submit.
     */
    private $type;

    /**
     * \brief Builds html hidden input.
     * \param name string that specifies the identifier for the HI.
     * \param value the string that store that value that requires to be hidden.
     * \note both parameters should be inserted else an error occurs.
     * \return html hidden input.
     */
    public function buildHiddenInput($name, $value) {
        if (isset($name) && isset($value)) {
            return "<INPUT TYPE=HIDDEN NAME=$name value=$value>";
        } else {
            return "Error, Parameters for hidden input are not set.";
        }
    }

    /**
     * \brief Builds html content for the simple dialog box.
     * \param id This is the unique html identifier for the simple dialog box.
     * \param title This specifies the title of the simple diaolog box.
     * \param message This is the actual message.
     * \param icon_type This specifies the jQuery UI icon.
     * \note id and title has to be set or else an error occurs. The other parameters are optional.
     * \return html content for the simple dialog box.
     */
    public function buildSimpleDialogueBox($id, $title, $message, $icon_type) {
        if (isset($id) && isset($title)) {
            if (isset($icon_type)) {
                return "<div id=$id title=\"$title\">
                   <p>
                   <span class=\"ui-icon $icon_type\" style='float:left; margin:0 7px 20px 0;'>
                   </span>
                   $message
                   </p>
                   </div>";
            } else {
                return "<div id=$id title=$title>
                   <p>
                   $message
                   </p>
                   </div>";
            }
        } else {
            return "Error, Parameters for Dialoqgue box are not set.";
        }
    }

    public function buildDialogueBoxWithContent($id, $title, $content_id, $content) {
        if (isset($id) && isset($title) && isset($content) && isset($content_id)) {

            return "<div id=$id title=\"$title\">
                   <div id=$content_id>
 $content
                   </div>
                   </div>";
        } else {
            return "Error, Parameters for Dialoqgue box are not set.";
        }
    }

    /**
     * \brief This builds the html content for set button type.
     * \param type This string specifies the button type.
     */
    public function setButtonType($type) {
        switch ($type) {
            case 'reset':
                $this->type = $type;
                break;
            case 'submit':
                $this->type = $type;
                break;
            default:
                $this->type = null;
                break;
        }
    }

    /**
     * \brief This builds the html content for button.
     * \param id This string sets the unique id for the button.
     * \param label This string specifies the button label.
     * \param class This string specifies class the button belongs to.
     */
    public function buildButton($id, $label, $class) {
        if ($id != null) {
            if ($this->type == 'reset' || $this->type == 'submit') {
                return "<button id=$id class=$class type=$this->type>$label</button>";
            } else {
                return "<button id=$id class=$class>$label</button>";
            }
        } else {
            echo "Error. Button parameters are not set.";
        }
    }

    /**
     * \brief This builds the html content for build text input.
     * \param id This string sets the unique id for the build text input.
     * \param default_text This string specifies default text in the text input.
     * \param class This string specifies class build text input belongs to.
     * \param with_label_bool This string specify whether to make it with a label '
     * or without a lable specified by True or false.
     * \param label_text This string specifies the label text for the text input.
     * \param label_orientation This string specifies the orientation of the orientation.
     * \param size This integer specifies the lenght of the text input.
     * \return This returns the build text input.
     */
    public function buildTextInput($id, $default_text, $class, $with_label_bool, $label_text, $label_orientation, $size='35') {

        if ($id == null) {
            return "Error. Text Input Parameters not defined.";
        }

        if ($with_label_bool == true) {
            if ($class == null) {
                $textInputUnderConstruction = "<input type=\"text\" id=\"$id\" size=$size value=\"$default_text\" />";
            } else {
                $textInputUnderConstruction = "<input type=\"text\" class=\"$class\" size=$size id=\"$id\" value=\"$default_text\" />";
            }
            switch ($label_orientation) {
                case 'top':
                    return "<label for=\"$id\">$label_text<br>$textInputUnderConstruction</label>";
                    ;
                    break;
                case 'left':
                    return "<label for=\"$id\">$label_text$textInputUnderConstruction</label>";
                    break;
                default:
                    return "<label for=\"$id\">$label_text$textInputUnderConstruction</label>";
                    break;
            }
        } else {
            if ($class == null) {
                return "<input type=\"text\" id=\"$id\" size=$size value=\"$default_text\" />";
            } else {
                return "<input type=\"text\" class=\"$class\" size=$size id=\"$id\" value=\"$default_text\" />";
            }
        }
    }

    /**
     * \brief This builds the html content for build text area.
     * \param id This string sets the unique id for the build text area.
     * \param default_text This string specifies default text in the text area.
     * \param class This string specifies class build area input belongs to.
     * \param with_label_bool This string specify whether to make it with a label '
     * or without a lable specified by True or false.
     * \param label_text This string specifies the label text for the text area.
     * \param label_orientation This string specifies the orientation of the
     *  orientation.
     * \param rows This integer specifies the number of rows needed for the build
     *  text area.
     * \param column This integer specifies the number of columns needed for the
     * build text area.
     * \return This returns the build text area.
     */
    public function buildTextArea($id, $default_text, $class, $with_label_bool, $label_text, $label_orientation, $rows='5', $columns='35') {

        if ($id == null) {
            return "Error. Text Input Parameters not defined.";
        }

        if ($with_label_bool == true) {
            if ($class == null) {
                $textAreaUnderConstruction = "<textarea id=\"$id\" rows=$rows cols=$columns>$default_text</textarea>";
            } else {
                $textAreaUnderConstruction = "<textarea id=\"$id\" class=$class rows=$rows cols=$columns>$default_text</textarea>";
            }
            switch ($label_orientation) {
                case 'top':
                    return "<label for=\"$id\">$label_text<br>$textAreaUnderConstruction</label>";
                    ;
                    break;
                case 'left':
                    return "<label for=\"$id\">$label_text$textAreaUnderConstruction</label>";
                    break;
                default:
                    return "<label for=\"$id\">$label_text$textAreaUnderConstruction</label>";
                    break;
            }
        } else {
            if ($class == null) {
                return "<textarea id=\"$id\" rows=$rows cols=$columns>$default_text</textarea>";
            } else {
                return "<textarea id=\"$id\" class=$class rows=$rows cols=$columns>$default_text</textarea>";
            }
        }
    }

    /**
     * \brief This builds the html content for build password input.
     * \param id This string sets the unique id for the password input.
     * \param default_text This string specifies default text in the password input.
     * \param class This string specifies class password input input belongs to.
     * \param with_label_bool This string specify whether to make it with a label '
     * or without a lable specified by True or false.
     * \param label_text This string specifies the label text for the password input.
     * \param label_orientation This string specifies the orientation of the
     *  orientation.
     * \param size This integer specifies the lenght of the password input
     * \return This returns the build text area.
     */
    public function buildPasswordInput($id, $default_text, $class, $with_label_bool, $label_text, $label_orientation, $size='35') {

        if ($id == null) {
            return "Error. Text Input Parameters not defined.";
        }

        if ($with_label_bool == true) {
            if ($class == null) {
                $passwordFieldUnderConstruction = "<input type=\"password\" size=$size id=\"$id\" value=\"$default_text\" />";
            } else {
                $passwordFieldUnderConstruction = "<input type=\"password\" size=$size class=\"$class\" id=\"$id\" value=\"$default_text\" />";
            }
            switch ($label_orientation) {
                case 'top':
                    return "<label for=\"$id\">$label_text<br>$passwordFieldUnderConstruction</label>";
                    ;
                    break;
                case 'left':
                    return "<label for=\"$id\">$label_text$passwordFieldUnderConstruction</label>";
                    break;
                default:
                    return "<label for=\"$id\">$label_text$passwordFieldUnderConstruction</label>";
                    break;
            }
        } else {
            if ($class == null) {
                return "<input type=\"password\" size=$size id=\"$id\" value=\"$default_text\" />";
            } else {
                return "<input type=\"password\" size=$size class=\"$class\" id=\"$id\" value=\"$default_text\" />";
            }
        }
    }

    /**
     * \brief This builds the html content for the add option functionality.
     * \param value This string sets the value of the add option.
     * \param label This string specifies the add option label.
     */
    public function addOption($value, $label) {
        if ($value == null) {
            return "Error. Radio Option Parameters Not Configured.";
        } else {
            $this->options[$value] = $label;
        }
    }

    /**
     * \brief This builds the html content set selected functionality.
     * \param value This string sets the value of the set selected.
     */
    public function setSelected($value) {
        if (isset($this->options[$value])) {
            $this->selected = $value;
        }
    }

    /**
     * \brief This builds the html content for the break space.
     * \param type This string sets the type of the break space.
     */
    public function setBreakSpace($type) {
        switch ($type) {
            case 'no space':
                $this->break_space = '';
                break;
            case 'new line':
                $this->break_space = '<br>';
                break;
            default:
                $this->break_space = '';
                break;
        }
    }

    /**
     * \brief This builds the html content for the drop down box.
     * \param dd_name This string sets the drop down name.
     * \param label_text This string sets the label for the drop down.
     * \param label_orientation This string sets the orientation of the drop down
     *  label.
     * \return  Returns the html content for the drop down
     */
    public function buildDropDown($dd_name, $label_text='', $label_orientation='top') {
        if (!empty($this->options)) {
            $ddUnderConstruction = "<select id =$dd_name>";

            foreach ($this->options as $opt => $lbl) {

                if ($this->selected == $opt) {
                    $ddUnderConstruction .= "<option selected='yes' value=$opt>$lbl</option>";
                } else {
                    $ddUnderConstruction .= "<option value=$opt>$lbl</option>";
                }
            }
            $ddUnderConstruction .= "</SELECT>";

            unset($this->options);

            if ($label_text != null) {
                switch ($label_orientation) {
                    case 'top':
                        return "<label for='$dd_name'>$label_text<div id='container_$dd_name'>" . $ddUnderConstruction . "</div></label>";
                        break;
                    case 'left':
                        return "<label for='$dd_name'>$label_text<span id='container_$dd_name'>" . $ddUnderConstruction . "</span></label>";
                        break;
                    default:
                        return "<label for='$dd_name'>$label_text<div id='container_$dd_name'>" . $ddUnderConstruction . "</div></label>";
                        break;
                }
            } else {
                return $constructedddElement = "<div id='$dd_name'>" . $ddUnderConstruction . "</div>";
            }
        } else {
            return "Error. Drop Down Options Available.";
        }
    }

    /**
     * \brief This builds the html content for the multiple drop down box.
     * \param msdd_name This string sets the multiple drop down name.
     * \param label_text This string sets label tesxt for the multiple selectable
     * drop down.
     * \param label_orientation This string sets the label orientation.
     * \param size This interger sopecifies the size of the multiple selectable
     *  drop down
     * \return  Returns the html content for the multiple selectable drop dwon
     */
    public function buildMultiSelectableDropDown($msdd_name, $label_text='', $label_orientation='top', $size='5') {
        if (!empty($this->options)) {
            $msddUnderConstruction = "<select id =$msdd_name MULTIPLE='multiple' SIZE=$size>";

            foreach ($this->options as $opt => $lbl) {

                $msddUnderConstruction .= "<option value=$opt>$lbl</option>";
            }
            $msddUnderConstruction .= "</SELECT>";

            unset($this->options);

            if ($label_text != null) {
                switch ($label_orientation) {
                    case 'top':
                        return "<label for='$msdd_name'>$label_text<div id='container_$msdd_name'>" . $msddUnderConstruction . "</div></label>";
                        break;
                    case 'left':
                        return "<label for='$msdd_name'>$label_text<span id='container_$msdd_name'>" . $msddUnderConstruction . "</span></label>";
                        break;
                    default:
                        return "<label for='$msdd_name'>$label_text<div id='container_$msdd_name'>" . $msddUnderConstruction . "</div></label>";
                        break;
                }
            } else {
                return $constructedmsddElement = "<div id='$msdd_name'>" . $msddUnderConstruction . "</div>";
            }
        } else {
            return "Error. No Multiselectable Drop Down Options Available.";
        }
    }

    /**
     * \brief This builds the html content for the multiple drop down box.
     * \param radio_name This string sets the name for the radio button.
     * \param label_text This string sets label tesxt for the radio button.
     * \param label_orientation This string sets the radio button orientation.
     * \return  Returns the html content for the radio button.
     */
    public function buildRadioButton($radio_name, $label_text='', $label_orientation='top') {


        if (!empty($this->options)) {
            $radioUnderConstruction = null;
            foreach ($this->options as $opt => $lbl) {
                if ($this->selected == $opt) {
                    $radioUnderConstruction .= "<input type=\"radio\" checked='checked' name=\"$radio_name\" id='$opt$radio_name' value=\"$opt\" ><label for='$opt$radio_name'>$lbl</label>$this->break_space";

                    // $radioUnderConstruction .= "sdfdfsd";
                } else {
                    $radioUnderConstruction .= "<input type=\"radio\" id='$opt$radio_name' name=\"$radio_name\" value=\"$opt\" ><label for='$opt$radio_name'>$lbl</label>$this->break_space";
                }
            }
            unset($this->options);

            if ($label_text != null) {
                switch ($label_orientation) {
                    case 'top':
                        return "<label for='$radio_name'>$label_text<div id='container_$radio_name'>" . $radioUnderConstruction . "</div></label>";
                        break;
                    case 'left':
                        return "<label for='$radio_name'>$label_text<span id='container_$radio_name'>" . $radioUnderConstruction . "</span></label>";
                        break;
                    default:
                        return "<label for='$radio_name'>$label_text<div id='container_$radio_name'>" . $radioUnderConstruction . "</div></label>";
                        break;
                }
            } else {
                return $constructedRadioElement = "<div id='container_$radio_name'>" . $radioUnderConstruction . "</div>";
            }
        } else {
            return "Error. No Radio Options Available.";
        }
    }

    /**
     * \brief This builds the html content for the check box.
     * \param id This interger sets the unique id of the check button.
     * \param label This string sets label text for the check button.
     * \return  Returns the html content for the check box.
     */
    public function buildCheckBox($id, $label) {
        if (isset($id) && isset($label)) {
            return "<input type='checkbox' id=$id /><label for=$id >$label</label>";
        } else {
            return "Error. Check Box Parameters are not set.";
        }
    }

}
?>
